# -*- coding: utf-8 -*-
import yaml
from ConfigUtil import ConfigIni
import AdbUtils as adbUtils
from SystemUtil import kill_pid

def get_device():
    '''
    :return: 获取已连接设备列表
    '''
    devices_list=[]
    adb=adbUtils.ADB()
    for device in adb.adb('devices').stdout.readlines():
        t=device.decode().split('\t')
        if len(t)>=2:
            devices_list.append(t[0])
    return devices_list


def get_ir_device(device_ip):
    '''
    :return: 获取红外事件接收设备,需要适配不同机器
    '''
    tv={"IR Receiver":158,'mtkinp_events':174,'aml_keypad':15,'Hi keyboard':158}
    adb=adbUtils.ADB(device_ip)
    device=""
    value=''
    p=adb.shell('getevent')
    # print("进程id",p.pid)
    out=p.stdout
    for i in range(30):
        try:
            t=out.readline().decode()
            if 'add device' in t:
                device=t.split(':')[1]
                continue
            if 'name' in t:
                value=t.split('name:')[1]
            for item in tv.items():
                key=item[0]
                if key in value:
                    kill_pid(p.pid)
                    return device.strip(),tv[key]
            # if "IR Receiver" in value :
            #     # os.killpg(os.getpgid(p.pid), 9)
            #     kill_pid(p.pid)
            #     return device.strip(),tv['IR Receiver']
            # elif "aml_keypad" in value:
            #     kill_pid(p.pid)
            #     return device.strip(),tv['aml_keypad']
            # elif 'mtkinp_events' in value:
            #     kill_pid(p.pid)
            #     return device.strip(), tv['mtkinp_events']
            # elif 'Hi keyboard' in value:
            #     kill_pid(p.pid)
            #     return device.strip(), tv['Hi keyboard']
        except:
            kill_pid(p.pid)
            return '/dev/input/event0',tv['IR Receiver']
    kill_pid(p.pid)
    return  '/dev/input/event0',tv['IR Receiver']


# tv={"Tcl Smart TV IR Receiver":158,'mtkinp_events':174,'aml_keypad':15}
#     adb=adbUtils.ADB(device_ip)
#     device=""
#     value=''
#     out=adb.shell('getevent').stdout
#     for i in range(15):
#         try:
#             t=out.readline().decode()
#             # print(t)
#             if 'add device' in t:
#                 device=t.split(':')[1]
#             if 'name' in t:
#                 value=t.split('name:')[1]
#             print(value)
#             if value in tv:
#                 return device.strip(),tv[value]
#         except:
#             pass
#     return "/dev/input/event0",158

def set_device_yaml():
    '''
    获取当前设备的Android 版本并保持到yaml下
    :return:
    '''
    device_list=[]
    for device in get_device():
        adb =adbUtils.ADB(device)
        print(
            'get device:{},Android version:{}'.format(
                device,adb.get_android_version()
            )
        )
        device_list.append({
            'platformVersion':adb.get_android_version(),
            'deviceName':device,
            'platformName':'Android'
        })

    ini=ConfigIni()
    # device_info_file=ini.get_ini('test_device','device')
    with open(ini.get_ini('test_device','device'),'w') as f:
        yaml.dump(device_list,f)


if __name__ == '__main__':
    print(get_ir_device('192.168.1.105:5555'))
    # tv = {"IR Receiver": 158, 'mtkinp_events': 174, 'aml_keypad': 15, 'Hi keyboard': 158}
    # # print(tv.keys())
    # for i in tv.items():
    #     print(i[0])


